package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.utils.HttpUtil;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.domain.DrugResolveTable;
import com.ruoyi.system.domain.DrugRuleResolve;
import com.ruoyi.system.domain.Table;
import com.ruoyi.system.domain.TableColumn;
import com.ruoyi.system.domain.vo.MatchVo;
import com.ruoyi.system.service.IDrugResolveTableService;
import com.ruoyi.system.service.IDrugRuleResolveService;
import com.ruoyi.system.service.ITableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 规则明细表Controller
 * 
 * @author wmb
 * @date 2020-07-28
 */
@RestController
@RequestMapping("/resolve")
public class DrugRuleResolveController extends BaseController
{
    @Autowired
    private IDrugRuleResolveService drugRuleResolveService;

    @Resource
    private ITableService tableService;

    @Resource
    private IDrugResolveTableService drugResolveTableService;
    /**
     * 查询规则明细表列表
     */
    @PreAuthorize("@ss.hasPermi('system:resolve:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrugRuleResolve drugRuleResolve)
    {
        startPage();
        List<DrugRuleResolve> list = drugRuleResolveService.selectDrugRuleResolveList(drugRuleResolve);
        return getDataTable(list);
    }

    /**
     * 导出规则明细表列表
     */
    @PreAuthorize("@ss.hasPermi('system:resolve:export')")
    @Log(title = "规则明细表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DrugRuleResolve drugRuleResolve) throws IOException
    {
        List<DrugRuleResolve> list = drugRuleResolveService.selectDrugRuleResolveList(drugRuleResolve);
        ExcelUtil<DrugRuleResolve> util = new ExcelUtil<DrugRuleResolve>(DrugRuleResolve.class);
        util.exportExcel(response, list, "resolve");
    }

    /**
     * 获取规则明细表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:resolve:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(drugRuleResolveService.selectDrugRuleResolveById(id));
    }

    /**
     * 新增规则明细表
     */
    @PreAuthorize("@ss.hasPermi('system:resolve:add')")
    @Log(title = "规则明细表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<DrugRuleResolve> drugRuleResolves)
    {
        for(DrugRuleResolve drugRuleResolve : drugRuleResolves){
            drugRuleResolveService.insertDrugRuleResolve(drugRuleResolve);
        }
        return toAjax(1);
    }

    /**
     * 修改规则明细表
     */
    @PreAuthorize("@ss.hasPermi('system:resolve:edit')")
    @Log(title = "规则明细表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DrugRuleResolve drugRuleResolve)
    {
        return toAjax(drugRuleResolveService.updateDrugRuleResolve(drugRuleResolve));
    }

    /**
     * 删除规则明细表
     */
    @PreAuthorize("@ss.hasPermi('system:resolve:remove')")
    @Log(title = "规则明细表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(drugRuleResolveService.deleteDrugRuleResolveByIds(ids));
    }


    @GetMapping("/db/list")
    public TableDataInfo dataList(Table table)
    {
        startPage();
        List<Table> list = tableService.selectDbTableList(table);
        return getDataTable(list);
    }


    @GetMapping("/db/columnList")
    public TableDataInfo columnList(String tableName)
    {
        startPage();
        List<TableColumn> tableColumns = tableService.selectDbTableColumnsByName(tableName);
        return getDataTable(tableColumns);
    }

    @GetMapping(value = "/ruleCategoryStat")
    public AjaxResult ruleCategoryStat()
    {
        return AjaxResult.success(drugRuleResolveService.ruleCategoryStat());
    }
}
